package com.muyu.system.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.system.domain.SysConfig;
import com.muyu.system.domain.model.SysConfigPageQueryModel;
import com.muyu.system.domain.req.SysConfigAddReq;
import com.muyu.system.domain.req.SysConfigListReq;
import com.muyu.system.domain.req.SysConfigUpdReq;
import com.muyu.system.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 参数配置 信息操作处理
 *
 * @author muyu
 */
@RestController
@RequestMapping("/config")
@Tag(name = "SysConfigController", description = "系统参数配置") 
public class SysConfigController extends BaseController {

    @Autowired
    private SysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @RequiresPermissions("system:config:list")
    @PostMapping("/list")
    @Operation(summary = "查询集合", description = "更新水果信息")
    public Result<DataPageResp<SysConfig>> list (@RequestBody SysConfigListReq sysConfigListReq) {
        PageQueryModel<SysConfig> pageQueryModel = configService.pageQuery(SysConfigPageQueryModel.reqBuild(sysConfigListReq));
        return resultList(pageQueryModel);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:config:export")
    @PostMapping("/export")
    public void export (HttpServletResponse response, SysConfigListReq sysConfigListReq) {
        PageQueryModel<SysConfig> pageQueryModel = configService.pageQuery(SysConfigPageQueryModel.reqBuild(sysConfigListReq));
        ExcelUtil<SysConfig> util = new ExcelUtil<>(SysConfig.class);
        util.exportExcel(response, pageQueryModel.getDataList(), "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{configId}")
    public Result<SysConfig> getInfo (@PathVariable("configId") Long configId) {
        return success(configService.getById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public Result<String> getConfigKey (@PathVariable("configKey") String configKey) {
        return success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @RequiresPermissions("system:config:add")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<String> add (@Validated @RequestBody SysConfigAddReq sysConfigAddReq) {
        if (!configService.checkConfigKeyUnique(sysConfigAddReq.getConfigKey())) {
            return error(StringUtils.format("新增参数「{}」失败，参数键名已存在",sysConfigAddReq.getConfigName()));
        }
        return toAjax(configService.save(SysConfig.addReqBuild(sysConfigAddReq)));
    }

    /**
     * 修改参数配置
     */
    @RequiresPermissions("system:config:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{sysConfigId}")
    public Result<String> edit (@PathVariable("sysConfigId")Long sysConfigId, @Validated @RequestBody SysConfigUpdReq sysConfigUpdReq) {
        if (!configService.checkConfigKeyUnique(sysConfigUpdReq.getConfigKey())) {
            return error("修改参数'" + sysConfigUpdReq.getConfigName() + "'失败，参数键名已存在");
        }
        return toAjax(configService.updateById(
                SysConfig.updReqBuild(sysConfigUpdReq, sysConfigId)
        ));
    }

    /**
     * 删除参数配置
     */
    @RequiresPermissions("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public Result<String> remove (@PathVariable("configIds") Long[] configIds) {
        configService.removeBatchByIds(Arrays.asList(configIds));
        return success();
    }

    /**
     * 刷新参数缓存
     */
    @RequiresPermissions("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public Result<String> refreshCache () {
        configService.resetConfigCache();
        return success();
    }
}
